package cn.taneroom.lucene.analyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.cn.smart.HMMChineseTokenizer;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.StopFilter;

public class SynonymAnalyzer extends Analyzer {

	private SynonymEngine engine;
	
	public SynonymAnalyzer(SynonymEngine engine){
		this.engine = engine;
	}
	
	@Override
	protected TokenStreamComponents createComponents(String fieldName) {
		Tokenizer source = new HMMChineseTokenizer();
		TokenStream result = new SynonymFilter(source, engine);
		//将字母转换成小写
		result = new LowerCaseFilter(result);
		//过滤掉禁止词
		result = new StopFilter(result, StopAnalyzer.ENGLISH_STOP_WORDS_SET);
		return new TokenStreamComponents(source, result);
	}

}
